﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Lapas.Common.Forms;
using WorkDAL;
using WorkDAL.Meterials;
using Lapas.Common;

namespace Material
{
    public partial class MaterialEditForm : EditForm
    {
        public LiMaterial liMaterial = new LiMaterial();

        public MaterialEditForm()
        {
            InitializeComponent();
            base.OkButton = btnOK;
            base.CloseButton = btnClose;
        }

        private void LookUp(bool checkIfEmpty)
        {
            txtMaterialGroupId.Text = LookupControl.LookupControl.Instance().LookUpControl("MaterialGroupId, MaterialGroupName, MortherGroupId",
                          "Mã nhóm, Tên nhóm, Nhóm mẹ", "LiMaterialGroup", "MaterialGroupId", txtMaterialGroupId.Text.Trim(), "Mã nhóm", checkIfEmpty);
        }

        private void txtMaterialGroupId_Validated(object sender, EventArgs e)
        {
            LookUp(false);
        }

        private void txtMaterialGroupId_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F5)
                LookUp(true);
        }

        protected override void BindData()
        {
            if (base.FormAction != EditFormAction.Insert)
            {
                txtMaterialGroupId.Text = liMaterial.MaterialGroupId;
                txtMaterialId.Text = liMaterial.MaterialId;
                txtMaterialName.Text = liMaterial.MaterialName;
                txtUnit.Text = liMaterial.Unit;
                txtUnitChange1.Text = liMaterial.UnitChange1;
                txtUnitChange2.Text = liMaterial.UnitChange2;
                txtUnitChangeRate1.Text = liMaterial.UnitChangeRate1.ToString();
                txtUnitChangeRate2.Text = liMaterial.UnitChangeRate2.ToString();
                txtBuyPrice.Text = liMaterial.BuyPrice.ToString(); ;
                txtSellPrice.Text = liMaterial.SellPrice.ToString();
            }
        }

        private void MaterialEditForm_Load(object sender, EventArgs e)
        {
            if (base.FormAction == EditFormAction.Change)
                txtMaterialId.Enabled = false;
        }

        private bool CheckData()
        {
            if (txtMaterialId.Text.Trim() == string.Empty)
            {
                erp.SetError(txtMaterialId, "Chưa nhập mã vật tư");
                return false;
            }

            return true;
        }

        protected override bool DoCreate()
        {
            try
            {
                if (!CheckData()) return false;

                LiMaterial liMaterial = new LiMaterial();

                liMaterial.MaterialId = txtMaterialId.Text.Trim();
                liMaterial.MaterialGroupId = txtMaterialGroupId.Text.Trim();
                liMaterial.MaterialName = txtMaterialName.Text.Trim();
                liMaterial.Unit = txtUnit.Text.Trim();
                liMaterial.UnitChange1 = txtUnitChange1.Text.Trim();
                liMaterial.UnitChange2 = txtUnitChange2.Text.Trim();

                if (!string.IsNullOrEmpty(txtUnitChangeRate1.Text.Trim()))
                    liMaterial.UnitChangeRate1 = Convert.ToDecimal(txtUnitChangeRate1.Text.Trim());

                if (!string.IsNullOrEmpty(txtUnitChangeRate2.Text.Trim()))
                    liMaterial.UnitChangeRate2 = Convert.ToDecimal(txtUnitChangeRate2.Text.Trim());

                if (!string.IsNullOrEmpty(txtBuyPrice.Text.Trim()))
                    liMaterial.BuyPrice = Convert.ToDecimal(txtBuyPrice.Text.Trim());

                if (!string.IsNullOrEmpty(txtSellPrice.Text.Trim()))
                    liMaterial.SellPrice = Convert.ToDecimal(txtSellPrice.Text.Trim());

                MaterialDataAccess.Instance().CreateMaterial(liMaterial);
            }
            catch (Exception ex)
            {
                DisplayMessage.ShowError(ex.Message);
            }

            return true;
        }


        protected override bool DoUpdate()
        {
            try
            {
                if (!CheckData()) return false;

                LiMaterial liMaterial = new LiMaterial();

                liMaterial.MaterialGroupId = txtMaterialGroupId.Text.Trim();
                liMaterial.MaterialId = txtMaterialId.Text.Trim();
                liMaterial.MaterialName = txtMaterialName.Text.Trim();
                liMaterial.Unit = txtUnit.Text.Trim();
                liMaterial.UnitChange1 = txtUnitChange1.Text.Trim();
                liMaterial.UnitChange2 = txtUnitChange2.Text.Trim();

                if (!string.IsNullOrEmpty(txtUnitChangeRate1.Text.Trim()))
                    liMaterial.UnitChangeRate1 = Convert.ToDecimal(txtUnitChangeRate1.Text.Trim());

                if (!string.IsNullOrEmpty(txtUnitChangeRate2.Text.Trim()))
                    liMaterial.UnitChangeRate2 = Convert.ToDecimal(txtUnitChangeRate2.Text.Trim());

                if (!string.IsNullOrEmpty(txtBuyPrice.Text.Trim()))
                    liMaterial.BuyPrice = Convert.ToDecimal(txtBuyPrice.Text.Trim());

                if (!string.IsNullOrEmpty(txtSellPrice.Text.Trim()))
                    liMaterial.SellPrice = Convert.ToDecimal(txtSellPrice.Text.Trim());

                MaterialDataAccess.Instance().UpdateMaterial(liMaterial);
            }
            catch (Exception ex)
            {
                DisplayMessage.ShowError(ex.Message);
            }

            return true;
        }


    }
}
